package com.schoolcardinfo.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.schoolcardinfo.beans.Id_Admin;
import com.schoolcardinfo.biz.IId_AdminBiz;
import com.schoolcardinfo.biz.impl.Id_AdminBizImpl;
import com.schoolcardinfo.utils.SessionNames;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PrintWriter out;
	private HttpSession session;
	private IId_AdminBiz adminBiz=new Id_AdminBizImpl();

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op=request.getParameter("op");
		session=request.getSession();
		out=response.getWriter();
		
		if("adminLogin".equals(op)){
			adminLogin(request,response);
		}
	}

	private void adminLogin(HttpServletRequest request,HttpServletResponse response) {
		String adminId=request.getParameter("adminId");
		String password=request.getParameter("pwd");
		Id_Admin ad=adminBiz.adminLogin(Integer.valueOf(adminId), password);
		session.setAttribute(SessionNames.ADMININFO,ad);
		JSONArray json=JSONArray.fromObject(ad);
		JSONObject jb=new JSONObject();
		jb.put("rows",json);
		out.print(jb.toString());
		out.flush();
		out.close();
	}
}
